package com.example.mobile2021606127cxz0415homework2;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainResult;
    private Button btnMainSupplier;
    private Button btnMainBuy;
    private Button btnMainExit;
    private Button btnMainTijiao;
    private String[] suppliers = {"新星汽车配件", "枫叶汽车配件", "天辉汽车配件", "西末汽车配件"};
    private String[] buy = {"轿车引擎", "轿车前玻璃", "轿车方向盘"};
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        tvMainResult.setText(null);
    }

    private void initView() {
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
        btnMainExit = (Button) findViewById(R.id.btn_main_exit);
        btnMainTijiao = (Button) findViewById(R.id.btn_main_tijiao);
    }

    public void exit(View view) {
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("是否确认退出");
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                MainActivity.this.finish();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    public void onClcik(View view) {
//        AlertDialog dialog;
//        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//        builder.setTitle("请选择供应商");
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                tvMainResult.setText(null);
//                dialog.dismiss();
//            }
//        });
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
//        builder.setSingleChoiceItems(suppliers, 0, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                tvMainResult.setText(suppliers[which]);
//
//            }
//        });
//        dialog = builder.create();
//        dialog.show();
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择商品");
        builder.setMultiChoiceItems(buy, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (int i = 0; i <= buy.length - 1; i++) {
                    if (listView.getCheckedItemPositions().get(i) && !tvMainResult.equals(null)) {

                        tvMainResult.append("\n" + buy[i]);
                    }
                }
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        listView = dialog.getListView();
        dialog.show();
    }
}